package com.example.aigc_education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.aigc_education.common.BusinessException;
import com.example.aigc_education.domain.Note;
import com.example.aigc_education.domain.NoteTag;
import com.example.aigc_education.domain.request.AddNoteTagRequest;
import com.example.aigc_education.domain.vo.NoteVO;
import com.example.aigc_education.mapper.NoteTagMapper;
import com.example.aigc_education.service.NoteTagService;
import com.example.aigc_education.service.converter.NoteConverter;
import com.example.aigc_education.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * NoteTag服务实现类
 */
@Service
public class NoteTagServiceImpl extends ServiceImpl<NoteTagMapper, NoteTag> implements NoteTagService {

    @Resource
    private NoteTagMapper noteTagMapper;

    @Resource
    private NoteConverter noteConverter;

    /**
     * 根据 userId 和 tagName 获取 Note
     * 使用 noteConverter 将 noteList 转换 noteVOList
     *
     * @param userId
     * @param tagName
     * @return 笔记集合
     */
    @Override
    public List<NoteVO> selectNoteByTagName(Long userId, String tagName) {

        try{

            List<Note> noteList = noteTagMapper.selectNotesByUserIdAndTagName(userId, tagName);
            List<NoteVO> noteVOList = noteConverter.noteListToNoteVOList(noteList);
            noteVOList = noteVOList.stream().peek(noteVO -> {

                Long noteId = noteVO.getNoteId();
                noteVO.setTags(noteTagMapper.selectTagsInNoteByNoteId(noteId));

            }).collect(Collectors.toList());

            return noteVOList;

        }catch (Exception e){
            System.out.println(e.getMessage());
            throw new BusinessException("获取note异常");
        }

    }

    /**
     * 根据用户id查询所有标签
     *
     * @param userId 用户id
     * @return 标签集合
     */
    @Override
    public List<String> selectTagsByUserId(Long userId) {
        try{

            LambdaQueryWrapper<NoteTag> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(NoteTag::getUserId, userId);
            List<NoteTag> noteTags = noteTagMapper.selectList(queryWrapper);
            List<String> tagNames = noteTags.stream().map(NoteTag::getTagName).collect(Collectors.toList());

            return tagNames;

        }catch (Exception e){
            throw new BusinessException("获取tags异常");
        }
    }

    /**
     * 根据笔记id查询标签名集合
     * @param noteId 笔记id
     * @return 标签名集合
     */
    @Override
    public List<String> selectTagsByNoteId(Long noteId) {
        try{
            return noteTagMapper.selectTagsByNoteId(noteId);
        }catch (Exception e){
            throw new BusinessException("获取tags异常");
        }
    }

    /**
     * 给笔记添加标签
     *
     * @param noteId    笔记id
     * @param tagId     标签id
     */
    @Override
    public void addNoteTag(Long noteId, Long tagId) {
        try{
            Long userId = SecurityUtils.getUserId();
            AddNoteTagRequest noteTag = new AddNoteTagRequest(noteId, tagId, userId);
            noteTagMapper.insertNoteTag(noteTag);

        }catch (Exception e){
            throw new BusinessException("添加标签异常");
        }
    }
} 